<?php
/**
 * created by zzy
 * date: 2017/11/17 10:39
 */


namespace App\Http\Admin\Process\Controllers;

use App\Eloquent\Ygt\ProcessFieldCategory;
use Framework\BaseClass\Http\Admin\Controller;


class CategoryController extends Controller
{
    public function lists()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        if (request()->isMethod('post')) {

            $where          = ['company_id'=>$companyId];
            $lists          = ProcessFieldCategory::getList( $where );
            $result         = collect($lists)->toJson();
            return $result;
        } else {
            return $this->view('lists');
        }
    }
    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $params             = $this->requestJson();
        $params['company_id']   = $companyId;
        $fieldList              = ProcessFieldCategory::$tableFieldList;
        $tableData              = ProcessFieldCategory::setDataByTableField($params,$fieldList);
        $tableDataCheckResult   = ProcessFieldCategory::checkDataByTableField($tableData,$fieldList);
        if($tableDataCheckResult !== true)
        {
            return $this->ajaxFail($tableDataCheckResult);
        }
        $result                 = ProcessFieldCategory::edit($tableData);
        if( $result ){
            return $this->ajaxSuccess('成功');
        } else {
            return $this->ajaxFail('失败');
        }
    }
    public function delete()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $id                 = request('id',0);
        if($id){
            $where          = ['company_id'=>$companyId,'id'=>$id];
            $delResult      = ProcessFieldCategory::del($where);
            if( $delResult ){
                return $this->ajaxSuccess('成功');
            } else {
                return $this->ajaxFail('失败');
            }
        }else{
            return $this->ajaxFail('失败');
        }
    }
}